package com.xintonghua.bussiness.ui.user.manage;

import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.xintonghua.base.utils.RefreshUtils;
import com.xintonghua.bussiness.EnumType;
import com.xintonghua.bussiness.R;
import com.xintonghua.bussiness.adapter.GoodAdapter;
import com.xintonghua.bussiness.base.BaseFragment;
import com.xintonghua.bussiness.bean.GoodsManageBean;
import com.xintonghua.bussiness.bean.GoodsManageResponse;
import com.xintonghua.bussiness.event.AddProductEventBus;
import com.xintonghua.bussiness.util.MyState;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * 作者：wanxianyi
 * 时间：2018/1/15.
 * 包名：com.xintonghua.bussiness.ui.user.manage
 */

public class GoodsManageFragment extends BaseFragment {

    @BindView(R.id.lv_goods)
    XRecyclerView lvGoods;
    Unbinder unbinder;
    GoodAdapter adapter;
    @BindView(R.id.tv_goods_all)
    TextView tvGoodsAll;
    @BindView(R.id.tv_goods_price)
    CheckBox tvGoodsPrice;
    @BindView(R.id.tv_goods_sales)
    TextView tvGoodsSales;
    @BindView(R.id.rl_goods_price)
    RelativeLayout rlGoodsPrice;
    private List<TextView> list;
    private int page = 1;
    private int type = EnumType.ALL;
    private List<GoodsManageBean> manageBeans = new ArrayList<>();
    GoodsManageResponse response;

    @Override
    public int getLayout() {
        return R.layout.fragment_goods_manage;
    }

    @Override
    public void initUI(View rootView) {
        unbinder = ButterKnife.bind(this, rootView);
        RefreshUtils.initList(getActivity(), lvGoods);
        adapter = new GoodAdapter(getActivity(), manageBeans);
        lvGoods.setAdapter(adapter);
        lvGoods.setLoadingListener(this);
        find();
        list = new ArrayList<>();
        list.add(tvGoodsAll);
        list.add(tvGoodsPrice);
        list.add(tvGoodsSales);
    }

    private void find() {
        httpCent.stockProducts(page, "", type,
                this, MyState.requestOne);
    }

    @Override
    public void dataBack(Object data, int order) {
        super.dataBack(data, order);
        switch (order) {
            case MyState.requestOne:
                if (page == 1) {
                    manageBeans.clear();
                }
                response = JSONObject.parseObject((String) data, GoodsManageResponse.class);
                manageBeans.addAll(response.getList());
                adapter.notifyDataSetChanged();
                RefreshUtils.onRefresh(adapter, lvGoods);
                break;

        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void onRefresh() {
        page = 1;
        find();
    }

    @Override
    public void onLoadMore() {
        if (RefreshUtils.hasNext(response, adapter, lvGoods)) {
            page++;
            find();
        }
    }


    @OnClick({R.id.tv_goods_all, R.id.rl_goods_price, R.id.tv_goods_sales})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_goods_all:
                setTextColor(0);
                type = EnumType.ALL;
                break;
            case R.id.rl_goods_price:
                type = EnumType.LITRE;
                setTextColor(1);
                if (type == EnumType.LITRE) {
                    type = EnumType.DROP;
                } else {
                    type = EnumType.LITRE;
                }
                tvGoodsPrice.setChecked(type == EnumType.LITRE ? true : false);
                break;
            case R.id.tv_goods_sales:
                setTextColor(2);
                type = EnumType.SALES;
                break;
        }
        find();
    }

    /**
     * 选中字体颜色
     *
     * @param postion
     */
    private void setTextColor(int postion) {
        for (int i = 0; i < list.size(); i++) {
            if (i == postion) {
                list.get(i).setTextColor(ContextCompat.getColor(getActivity(), R.color.red));
            } else {
                list.get(i).setTextColor(ContextCompat.getColor(getActivity(), R.color.black));
            }
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(AddProductEventBus eventBus) {
        find();
    }
}
